/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.asportoonline.tags;

import java.util.List;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import org.asportoonline.entities.Locale;

/**
 *
 * @author Luca
 */
public class LocaliVenditoreTag extends SimpleTagSupport {
    private List<Locale> locali;

    /**
     * Get the value of locali
     *
     * @return the value of locali
     */
    public List<Locale> getLocali() {
        return locali;
    }

    /**
     * Set the value of locali
     *
     * @param locali new value of locali
     */
    public void setLocali(List<Locale> locali) {
        this.locali = locali;
    }

    /**
     * Called by the container to invoke this tag. 
     * The implementation of this method is provided by the tag library developer,
     * and handles all tag processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();
        List<Locale> lista = getLocali();
        Locale l;
        String link;
        String seconda_apertura;
        String[] chiusura = {"Nessuno", "Luned&igrave;", "Marted&igrave;", "Mercoled&igrave;", "Gioved&igrave;", "Venerd&igrave;", "Sabato", "Domenica"};
        
        try {
            
            if(lista==null || lista.isEmpty()) {
                out.print("<p>Nessun locale registrato!!</p>");
            }
            else {
                out.print("<table><tr><th>NOME LOCALE</th>"
                        + "<th>VIA</th><th>CAP</th><th>CITTA</th><th>TELEFONO</th><th>EMAIL LOCALE</th>"
                        + "<th>PRIMA APERTURA</th><th>SECONDA APERTURA</th><th>DISTANZA MASSIMA</th>"
                        + "<th>GIORNO DI CHIUSURA</th><th>PARTITA IVA</th><th>IMMAGINE</th>"
                        + "</tr>");
                
                for(int i=0; i<lista.size(); i++) {
                    l = lista.get(i);
                    
                    if(l.getSecondaApertura()!=null && !l.getSecondaApertura().isEmpty()) {
                        seconda_apertura = l.getSecondaApertura();
                    }
                    else {
                        seconda_apertura = "-";
                    }
                    
                    link = "MainController?q=fetch_restaurant_details_action&AMP;ret=restaurant_details&AMP;idlocale="+l.getIdLocale();
                    out.print("<tr><td><a href=\""+link+"\">" + l.getNome() + "</a></td>"
                            + "<td>" + l.getIndirizzo().getVia() + "</td>"
                            + "<td>" + l.getIndirizzo().getCap() + "</td>"
                            + "<td>" + l.getIndirizzo().getCitta() + "</td>"
                            + "<td>" + l.getTelefono() + "</td>"
                            + "<td>" + l.getEmail() + "</td>"
                            + "<td>" + l.getPrimaApertura() + "</td>"
                            + "<td>" + seconda_apertura + "</td>"
                            + "<td>" + l.getDistanzaMassima() + " km</td>"
                            + "<td>" + chiusura[l.getGiornoChiusura()+1] + "</td>"
                            + "<td>" + l.getPiva() + "</td>"
                            + "<td><img src=\"img/" + l.getUrlimmagine() + "\" alt=\"locale_img\" height=\"50\" width=\"50\" /></td>"
                            + "</tr>");
                }
                
                out.print("</table>");
                out.flush();
            }
        } catch (java.io.IOException ex) {
            throw new JspException("Error in LocaliTag tag", ex);
        }
    }
}
